package com.junzhiit.nengji.shop.service.impl;

import java.io.File;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.bainaeco.framework.util.HttpClientUtils;
import com.junzhiit.nengji.dao.UserConfigDAO;
import com.junzhiit.nengji.dao.UserDAO;
import com.junzhiit.nengji.dao.WechatConfigDAO;
import com.junzhiit.nengji.dao.cache.WechatConfigCacheDAO;
import com.junzhiit.nengji.dao.util.DIYKey;
import com.junzhiit.nengji.model.User;
import com.junzhiit.nengji.model.UserConfig;
import com.junzhiit.nengji.model.WechatConfig;
import com.junzhiit.nengji.shop.service.UserConfigService;
import com.junzhiit.nengji.shop.web.util.wechat.WeChatUtils;


@Service
public class UserConfigServiceImpl extends BaseServiceImpl implements UserConfigService {

	@Autowired
	private UserConfigDAO userConfigDAO;
	
	@Autowired
	private UserDAO userDAO;
	
	@Autowired
	private WechatConfigCacheDAO wechatConfigCacheDAO;
	
	@Autowired
	private WechatConfigDAO wechatConfigDAO;
	
	@Value("${images.user.qrcode.path}")
	private String QR_CODE_PATH;
	
	@Value("${upload.mall.user.avatarurl}")
	private String userAvatarurlPath;
	
	@Value("${upload.temp}")
	private String uploadPath;
	
	//阿里云API的bucket名称
	@Value("${oss.backet.name}")
	private String backetName;
	
	public UserConfig  qrcode(String userId,String avatarUrl,String wcId) {
		UserConfig userConfig = userConfigDAO.getUserConfig(userId);
		if(userConfig == null){
			userConfig = new UserConfig();
			userConfig.setUserId(userId);
			UserConfig newUserConfig = userConfigDAO.insert(userConfig);
			userConfig.setId(newUserConfig.getId());
		}
		if(userConfig!=null && StringUtils.isBlank(userConfig.getQrCode())){
			User user = userDAO.query(userId);
			//保存头像到本地
			try {
				File file = new File(userAvatarurlPath);  
				if(!file.exists()){  
				    file.mkdirs();  
				} 
				HttpClientUtils.imageDownloader(user.getAvatarUrl(),userAvatarurlPath+user.getId()+".png");
			} catch (Exception e) {
				e.printStackTrace();
			}
			String key = DIYKey.getWechatCodeKey(wcId);
			WechatConfig wechatConfig = wechatConfigCacheDAO.getHash(key, WechatConfig.class);
			if(wechatConfig == null){
				wechatConfig = wechatConfigDAO.getWechatConfigByWcId(wcId);
				wechatConfigCacheDAO.addHash(key, wechatConfig);
			}
			String fileName = userId + ".png";
			WeChatUtils.wxQRCode(backetName,user.getCode(), user.getId(), QR_CODE_PATH,fileName,wechatConfig);
			userConfig.setQrCode(fileName);
			userConfigDAO.update(userConfig);
			//WeChatUtils.replaceLogo(QR_CODE_PATH,userAvatarurlPath,uploadPath,fileName);
		}
		return userConfig;
	}
	
	/*public ResultVO getAvatarur(){
		User user = new User();
		List<User> list = userDAO.findByUser(user);
		try {
			for(User params : list){
				//UserConfig userConfig = userConfigDAO.getUserConfig(params.getId());
				if(StringUtils.isNoneBlank(params.getAvatarUrl())){
					HttpClientUtils.imageDownloader(params.getAvatarUrl(),userAvatarurlPath+params.getId()+".png");
					String fileName = params.getId() + ".png";
					System.err.println("------------------>"+fileName);
					//WeChatUtils.wxQRCode(user.getCode(), user.getId(), QR_CODE_PATH,fileName);
					//userConfig.setQrCode(fileName);
					//userConfigDAO.update(userConfig);
					WeChatUtils.replaceLogo(QR_CODE_PATH,userAvatarurlPath,uploadPath,fileName);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}*/
}
